package com.zwps.biz.controller.http;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zwps.biz.api.model.dto.role.AddRoleDTO;
import com.zwps.biz.api.model.dto.role.AddRolePermissionApiDTO;
import com.zwps.biz.api.model.dto.role.SelectRoleDTO;
import com.zwps.biz.api.model.dto.role.UpdateRoleDTO;
import com.zwps.biz.api.model.vo.permission.api.SelectPermissionApiVO;
import com.zwps.biz.api.model.vo.role.RoleVO;
import com.zwps.biz.api.model.vo.role.UpdateRoleVO;
import com.zwps.biz.api.model.vo.user.UserVO;
import com.zwps.biz.dal.db.dao.UserRoleDAO;
import com.zwps.biz.dal.db.object.RoleDO;
import com.zwps.biz.domain.converter.RoleConverter;
import com.zwps.biz.domain.service.RoleService;
import com.zwps.common.api.model.dto.PageInfoDTO;
import com.zwps.common.controller.CommonController;
import com.zwps.common.controller.response.message.ResponseDataPageMessage;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 系统角色
 * 
 * @ClassName: RoleController
 * @Description:
 * @author lyh
 * @date 2023年5月22日 下午5:24:57
 *
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("system/role")
@Validated
public class RoleController extends CommonController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleDAO userRoleDAO;
    
    @Operation(summary = "新增角色")
    @PostMapping
    public void addRole(@Valid @NotNull @RequestBody AddRoleDTO dto) {
        roleService.addRole(dto);
    }

    @Operation(summary = "分页查询角色")
    @GetMapping("page")
    public ResponseDataPageMessage<RoleVO> selectRolePage(SelectRoleDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<RoleDO> page = (Page<RoleDO>) roleService.selectRoles(dto);
        return ResponseDataPageMessage.data(page, RoleConverter.INSTANCE.toRoleVOList(page.getResult()));
    }

    @Operation(summary = "查询全部角色")
    @GetMapping("option")
    public List<RoleVO> selectAllRolePage(SelectRoleDTO dto) {
        List<RoleDO> list = roleService.selectRoles(dto);
        List<RoleVO> voList = RoleConverter.INSTANCE.toRoleVOList(list);
        return voList;
    }

    @Operation(summary = "分页查询用户列表")
    @GetMapping("{roleId}/user/page")
    public List<UserVO> selectUserPage(@Valid @NotBlank @PathVariable String roleId, PageInfoDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return userRoleDAO.selectAllUserByRoleId(roleId);
    }
    
    @Operation(summary = "删除角色")
    @DeleteMapping("{ids}")
    public void deleteRoles(@Valid @NotBlank @PathVariable String ids) {
        roleService.deleteRoles(ids);
    }

    @Parameter(name = "id", description = "角色ID", in = ParameterIn.PATH)
    @Operation(summary = "修改角色信息")
    @PutMapping("{id}")
    public void updateRole(@Valid @NotBlank @PathVariable String id, @Valid @NotNull @RequestBody UpdateRoleDTO dto) {
        roleService.updateRole(id, dto);
    }

    @Operation(summary = "根据id查询角色信息")
    @GetMapping("{roleId}/page")
    public UpdateRoleVO selectRoleById(@Valid @NotBlank @PathVariable String roleId) {
        return roleService.selectRoleById(roleId);
    }

    @Operation(summary = "接口权限控制")
    @PostMapping("{roleId}/authority")
    public void authorityRolePermission(@Valid @NotBlank @PathVariable String roleId,
                                        @Valid @NotNull @RequestBody AddRolePermissionApiDTO dto) {
        roleService.authorityRolePermission(roleId,dto);
    }

    @Operation(summary = "根据角色id查询所属接口权限")
    @GetMapping("{roleId}/belongAuthority")
    public List<SelectPermissionApiVO> belongAuthorityRolePermission(@Valid @NotBlank @PathVariable String roleId) {
        return roleService.selectBelongAuthorityRolePermission(roleId);
    }
}
